package com.tao.gulimall.product.controller;

import com.tao.common.utils.PageUtils;
import com.tao.common.utils.R;
import com.tao.gulimall.product.entity.AttrEntity;
import com.tao.gulimall.product.entity.AttrGroupEntity;
import com.tao.gulimall.product.service.AttrAttrgroupRelationService;
import com.tao.gulimall.product.service.AttrGroupService;
import com.tao.gulimall.product.service.AttrService;
import com.tao.gulimall.product.service.CategoryService;
import com.tao.gulimall.product.vo.AttrGroupRelationVo;
import com.tao.gulimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 属性分组
 *
 * @author htt
 * @email 2967074103@qq.com
 * @date 2024-02-22 15:32:12
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Resource
    private AttrService attrService;

    @Resource
    private AttrAttrgroupRelationService relationService;

    /**
     * 添加属性关联, 批量添加
     */
    @PostMapping("/attr/relation")
    public R attrRelation(@RequestBody List<AttrGroupRelationVo> vos){
        relationService.addRelation(vos);
        return R.ok();
    }


    /**
     * 根据分组ID 查询关联属性
     */
    @GetMapping("/{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrGroupId") Long attrGroupId){
        List<AttrEntity> list = attrService.getRelaAttr(attrGroupId);

        return R.ok().put("data", list);
   }

    /**
     * 查询当前分类下的所有属性分组和每个分组下的所有属性
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        // 调用service方法执行业务逻辑,
        // 查询当前分类下的所有属性分组和每个分组下的所有属性
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrs(catelogId);
        return R.ok().put("data", vos);

    }



    /**
     * 查询所有没有被关联的属性
     */
    @GetMapping("/{attrGroupId}/noattr/relation")
    public R noRelation(@PathVariable("attrGroupId") Long attrGroupId,
                       @RequestParam Map<String, Object> params){
        PageUtils page = attrService.getNoRelaAttr(params, attrGroupId);
        return R.ok().put("page", page);
   }

    /**
     * 删除关联
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos){
        attrService.deleteRelation(vos);
        return R.ok();
    }



    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    // @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    // @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        // 查询出完整的路径并赋值
        Long[] path = categoryService.findCatelogPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
